<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        canvas {
            border: 1px solid;
        }

        input {
            display: block;
        }

        ul {
            list-style: none;
            display: flex;
            padding: 0;
        }

        ul>li {
            width: 30px;
            height: 20px;
            border: 2px solid #ccc;
            margin-left: 5px;
        }

        ul>li:nth-child(1) {
            background-color: red;
        }

        ul>li:nth-child(2) {
            background-color: orange;
        }

        ul>li:nth-child(3) {
            background-color: yellow;
        }

        ul>li:nth-child(4) {
            background-color: green;
        }

        ul>li:nth-child(5) {
            background-color: skyblue;
        }
    </style>
</head>

<body>
    <p>请签字：</p>
    <canvas width='500' height="500"></canvas>
    <ul>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
        <li></li>
    </ul>
    <input type="range"><span></span>
    <input type="button" value="擦除">

    <input type="color">


    <script>
        const cvs = document.querySelector('canvas');
        const ul = document.querySelector('ul'); // ul
        const btn = document.querySelector('[type="button"]'); // 按钮
        const ctx = cvs.getContext('2d');
        const range = document.querySelector('[type="range"]'); // range

        const colorPicker = document.querySelector('[type="color"]'); // color

        colorPicker.onchange = function () {
            // console.log(colorPicker.value);
            ctx.strokeStyle = colorPicker.value;
        }



        range.onchange = function () {
            let value = Math.ceil(range.value / 10);
            ctx.lineWidth = value;
            value == 0 ? value = 1 : '';
            this.nextElementSibling.innerText = value;
            ctx.lineCap = 'round';
        }


        cvs.onmousedown = function (event) {
            ctx.beginPath();
            let startX = event.offsetX, startY = event.offsetY;
            ctx.moveTo(startX, startY);
            cvs.onmousemove = function (event) {
                let x = event.offsetX, y = event.offsetY;
                ctx.lineTo(x, y);
                ctx.stroke();
            }
            cvs.onmouseup = function () {
                cvs.onmousemove = null;
            }
        }

        btn.onclick = function () {
            ctx.clearRect(0, 0, cvs.width, cvs.height);
        }

        ul.onclick = function (event) {
            let color = getComputedStyle(event.target).backgroundColor;
            ctx.strokeStyle = color;
            // event.target
        }

    </script>
</body>

</html>